package br.com.bb.cslsp.ejb.dao.usuario;

import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import br.com.bb.cslsp.dao.DAO;
import br.com.bb.cslsp.modelo.usuario.Usuario;

@Stateless
public class UsuarioDAO extends DAO<Usuario>{
	
	@PersistenceContext(unitName="apps")
	private EntityManager em;
	
	public Usuario buscaPorChave(String chave) throws Exception{
		Query query = em.createQuery("select u from Usuario u where u.chave =:chave")
				.setParameter("chave", chave);
		return (Usuario) query.getSingleResult();
	}
	
	public boolean existePorChave(String chave) {
		Query query = em.createQuery("select u from Usuario u where u.chave =:chave")
				.setParameter("chave", chave);
		return !query.getResultList().isEmpty();
	}
	
	@PostConstruct
	public void init() {
		super.setEm(em);
	}

}
